import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/my/help/feedback_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import 'package:sports_new/widgets/loading/no_data_widget.dart';
import '../../../server/api/user_api.dart';
import '../../../utils/routes.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/card.dart';
import '../widgets/my_appbar.dart';

class FeedbackHistoryPage extends StatefulWidget {
  const FeedbackHistoryPage({super.key});

  @override
  State<FeedbackHistoryPage> createState() => _FeedbackHistoryPageState();
}

class _FeedbackHistoryPageState extends State<FeedbackHistoryPage> {
  List<FeedbackEntity>? history;

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async {
    history = [];
    history = await UserApi.getFeedbackHistory();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colours.greyF2F3F7,
        appBar: const MyAppBar(title: "反馈记录"),
        body: SizedBox(
          height: Get.height,
          child: LoadingWidget<bool>(
            isLoading: history == null,
            data: history?.length == 0,
            noData: const NoDataWidget(needScroll: true),
            child: () => SingleChildScrollView(
              child: Column(
                children: history?.map((e) => item(e)).toList() ?? []
              )
            ),
          ),
        )
    );;
  }

  Widget item(FeedbackEntity? entity){
    return GestureDetector(
      onTap: () => Get.toNamed(Routes.feedbackDetail,arguments: entity),
      child: Block(
        cardStyle: const CardStyle(
          margin: EdgeInsets.all(8),
          padding: EdgeInsets.all(12),
        ),
        child: DefaultTextStyle(
          style: const TextStyle(fontSize: 14,color: Colours.text),
          child: Column(
            children: [
              Row(
                children: [
                  QkdText(
                    DateTime.tryParse(
                        entity?.createTime ?? ""
                    )?.formatedString('yyyy-MM-dd HH:mm') ?? "",
                    fontSize: 12,color: Colours.grey99
                  ),
                  const Spacer(),
                  QkdText(entity?.status==0?"未回复":"已回复"),
                  Container(width: 6),
                  Image.asset(Utils.getImgPath("arrow_right.png"),width: 6,height: 10)
                ],
              ).paddingOnly(bottom: 12),
              const Divider(color: Colours.greyEE,height: 0.5),
              textLine("问题类型：", entity?.typeName ?? "").paddingOnly(bottom: 8,top: 10),
              textLine("问题描述：", entity?.suggest ?? "").paddingOnly(bottom: 8),
              if(entity?.reply.hasValue == true)
              textLine("处理结果：", entity!.reply!).paddingOnly(bottom: 4)
            ],
          ),
        )
      ),
    );
  }

  Widget textLine(String title,String content){
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        QkdText(title,color: Colours.grey99),
        Expanded(
          child: !content.contains("<")?QkdText(content,maxLines: 4):Html(
            doNotRenderTheseTags: const {"img"},
            data: content,
            style: {
              "body": Style(
                margin: Margins.zero,
                padding: HtmlPaddings.zero
              ),
              "p": Style(
                margin: Margins.zero,
                padding: HtmlPaddings.zero,
                fontSize: FontSize(14),
                lineHeight: const LineHeight(1.35),
                color: Colours.text,
                maxLines: 4,
                textOverflow: TextOverflow.ellipsis
              )
            },
          )
        )
      ],
    );
  }
}
